package springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import springboot.bean.Task;
import springboot.bean.TaskExample;
import springboot.dao.TaskDAO;

import java.util.List;

@RestController
public class TaskController {

    @Autowired
    private TaskDAO taskDAO;

    @PostMapping("/createTask")
    public String createTask(@RequestBody Task task){
        int result=taskDAO.insert(task);
        if (result==1){
            return "success";
        }else {
            return "failed";
        }
    }

    TaskExample txList=null;
    TaskExample.Criteria criteriaList=null;
    List<Task> startList=null;
    @GetMapping("/getCount")
    public Integer getCount(String start){
        txList=new TaskExample();
        criteriaList=txList.createCriteria();
        criteriaList.andStartEqualTo(start);
        startList=taskDAO.selectByExample(txList);
        if (startList.size()==0){
            return 0;
        }else if (startList.size()==1){
            if (startList.get(0).getStatus()==3||startList.get(0).getStatus()==4){
                return 0;
            }else {
                return 1;
            }
        }else if (startList.size()==2){
                return 1;
        }
        return 1;
    }
}
